package com.cool.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.cool.dto.request.GetSysMenuListRequest;
import com.cool.dto.request.InsertSysMenuRequest;
import com.cool.dto.request.UpdateSysMenuRequest;
import com.cool.dto.response.CommonResponse;
import com.cool.dto.response.GetSysMenuListResponse;
import com.cool.dto.response.GetSysMenuResponse;
import com.cool.dto.response.InsertSystemResponse;
import com.cool.mybatisplus.SysMenuDaoService;
import com.cool.mybatisplus.entity.SystemMenu;
import com.cool.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 系统服务
 * 
 * @author bian
 * @date 2021-02-10
 */
@RestController
@Slf4j
public class SystemController {
    @Autowired
    private SysMenuDaoService sysMenuDaoService;

    /**
     * 新建系统菜单
     *
     * @param insertSysMenuRequest 插入请求对象
     * @return 插入返回响应
     */
    @PostMapping(value = "/system/menu/insert", produces = MediaType.APPLICATION_JSON_VALUE)
    public InsertSystemResponse insert(@RequestBody InsertSysMenuRequest insertSysMenuRequest) {
        InsertSystemResponse response = new InsertSystemResponse();
        ResultUtil.setSuccessResponse(response);
        SystemMenu systemMenu = SystemMenu.builder().createTime(LocalDateTime.now()).build();
        BeanUtils.copyProperties(insertSysMenuRequest, systemMenu);
        // sysMenuDaoService.insertSysMenu(systemMenu);
        sysMenuDaoService.insert(systemMenu);
        response.setMenuId(systemMenu.getMenuId());
        return response;
    }

    /**
     * 更新系统菜单
     *
     * @param menuId 菜单ID
     * @param updateSysMenuRequest 更新菜单请求体
     * @return 更新返回响应
     */
    @PutMapping(value = "/system/menu/update/{menuId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse update(@PathVariable("menuId") Long menuId, @RequestBody UpdateSysMenuRequest updateSysMenuRequest) {
        CommonResponse response = new CommonResponse();
        ResultUtil.setSuccessResponse(response);
        SystemMenu systemMenu = sysMenuDaoService.selectById(menuId);
        if (Objects.isNull(systemMenu)) {
            ResultUtil.setFailResponse(response);
            return response;
        }
        BeanUtils.copyProperties(updateSysMenuRequest, systemMenu);
        systemMenu.setUpdateTime(LocalDateTime.now());
        sysMenuDaoService.updateById(systemMenu);
        return response;
    }

    /**
     * 删除系统菜单
     *
     * @param menuId 菜单ID
     * @return 删除返回响应
     */
    @DeleteMapping(value = "/system/menu/delete/{menuId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse delete(@PathVariable("menuId") Long menuId) {
        CommonResponse commonResponse = new CommonResponse();
        ResultUtil.setSuccessResponse(commonResponse);
        sysMenuDaoService.deleteById(menuId);
        return commonResponse;
    }

    /**
     * 获取系统菜单详情
     * 
     * @param menuId 菜单ID
     * @return 系统菜单详情
     */
    @GetMapping(value = "/system/menu/get/{menuId}")
    public GetSysMenuResponse get(@PathVariable("menuId") Long menuId) {
        GetSysMenuResponse getSysMenuResponse = new GetSysMenuResponse();
        ResultUtil.setSuccessResponse(getSysMenuResponse);
        SystemMenu systemMenu = sysMenuDaoService.selectById(menuId);
        getSysMenuResponse.setSystemMenu(systemMenu);
        return getSysMenuResponse;
    }

    /**
     * 获取系统菜单列表
     * 
     * @param getSysMenuListRequest 查询系统菜单请求体
     * @return 系统菜单列表
     */
    @GetMapping(value = "/system/menu/get/list")
    public GetSysMenuListResponse getList(@RequestBody GetSysMenuListRequest getSysMenuListRequest) {
        GetSysMenuListResponse getSysMenuListResponse = new GetSysMenuListResponse();
        ResultUtil.setSuccessResponse(getSysMenuListResponse);
        List<SystemMenu> systemMenus = sysMenuDaoService.selectBatchIds(getSysMenuListRequest.getMenuIds());
        List<String> list = new ArrayList<>();
        // list.add("system");
        list.add("monitor");
        List<SystemMenu> systemMenus2 = sysMenuDaoService.selectByName(list);
        getSysMenuListResponse.setSystemMenuList(systemMenus);
        return getSysMenuListResponse;
    }

}
